package com.xyz.keshe.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xyz.keshe.R;
import com.xyz.keshe.model.Comment;

import java.util.List;

public class CommentAdapter extends ArrayAdapter<Comment> {

    private Context context;
    private List<Comment> commentList;

    public CommentAdapter(@NonNull Context context, List<Comment> commentList) {
        super(context, R.layout.item_comment, commentList);
        this.context = context;
        this.commentList = commentList;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_comment, parent, false);
            holder = new ViewHolder();
            holder.tvUsername = convertView.findViewById(R.id.tv_username);
            holder.tvContent = convertView.findViewById(R.id.tv_content);
            holder.tvDate = convertView.findViewById(R.id.tv_date);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Comment comment = commentList.get(position);
        holder.tvUsername.setText(comment.getUserName());
        holder.tvContent.setText(comment.getContent());
        holder.tvDate.setText(comment.getCreateTime());

        return convertView;
    }

    private static class ViewHolder {
        TextView tvUsername;
        TextView tvContent;
        TextView tvDate;
    }
} 